import share from './components/i-share-popup.vue'
import giftBag from '@/components/text-agreement/giftBag.vue'
import aggre from '../agreement/agreement.vue'
export default {
  components: {
    share,
    giftBag,
    aggre
  },
  data() {
    return {
      tips: '获取验证码',
      seconds: 60,

      isCode: true,
      //导航栏背景
      background: {
        backgroundColor: '#434667',
      },
      isShow: false, //模态框
      values: false, //是否勾选
      popupShow: false, //分享
      id: '', //提交订单id
      list: {}, //详情列表
      timestamp: 0, //毫秒
      img: '', //传过来的img
      total: 0, //爱车数量
      invitationCode: null, //邀请码
      orderId: ''
    }
  },

  created() {
    this.$nextTick(()=>{
      let token = uni.getStorageSync('token')
      if (token) {
        this.getCurrentUsersInviteCode()
      }
      this.getBuyDiscount()
    })
  },
  onLoad(op) {
    uni.setStorageSync('invitationcode', op.code)
    let code = op.code
    this.id = op.id
    // this.img = op.img
  },
  methods: {
    async getCurrentUsersInviteCode() {
      try {
        const res = await this.$api.me.getCurrentUsersInviteCode()
        console.log(res.data)
        this.invitationCode = res.data
      } catch (e) {
        //TODO handle the exception
      }
    },
    goPage() {
      uni.navigateTo({
        url: '/pages_buy_index/createCar/createCar'
      })
    },
    price(val, vla2) {
      return (val - vla2).toFixed(2)
    },
    //登录
    login() {
      uni.showTabBar({
        animation: true
      })
      uni.setStorage({
        key: 'launchFlag',
        data: false,
      })
      uni.navigateTo({
        url: '/pages_logins/login/login'
      })
    },
    //详情
    async getBuyDiscount() {
      try {
        let data = {
          'id': this.id
        }
        const res = await this.$api.maintenanceIndex.getBuyDiscountsAllDetailsById(data)

        this.list = res.data
        // const end = Date.parse(new Date(res.data.deadline))
        // const start = Date.parse(new Date())
        // this.timestamp = (end - start) / 1000
      } catch (e) {
        //TODO handle the exception
      }
    },

    //提交提单
    async addBuyDiscountsOrders() {
      try {
        let data = {
          'id': this.id
        }
        const res = await this.$api.maintenanceIndex.addBuyDiscountsOrders(data)
        let list = res

        if (res.code === 200) {
          uni.navigateTo({
            url: '/pages_commodity/payment/payment?orderNo=' + list.data + '&price=' + this.list.discount_Price +
							'&type=buyDiscountsOrder' + '&id=' + this.id + '&title=' + this.list.package_Title
          })
        }
      } catch (e) {
        //TODO handle the exception
      }

    },

    // 去支付
    goPay(id) {
      let token = uni.getStorageSync('token')
      if (!token) {
        this.$u.toast('请登录！')
        this.$u.debounce(this.login, 1000)
        return false
      } else {
        const start = Date.parse(new Date())
        const end = Date.parse(this.list.deadline)
        if(start>end){
          uni.showToast({
            title: '该福利购已过期',
            icon: 'none'
          })
					 return false
        }
        console.log(start<end)
        if (this.values === false) {
          // this.isShow = true
          uni.showToast({
            title: '请同意说明及规定后支付',
            icon: 'none'
          })
        } else {
          /* if(this.total===0){
            uni.navigateTo({
						  url: '/pages_buy_index/createCar/createCar?id='+id
            })
          }else{ */
          // uni.navigateTo({
          //   url: '/pages_commodity/myLoveCar/myLoveCar'
          // })
          this.addBuyDiscountsOrders()
          // }
        }
      }

    },
    //模态（确定）
    confirm(info) {
      setTimeout(() => {
        uni.$emit('agreements', info)
      },1)
      this.values = true
      uni.navigateTo({
			  url: '/pages_buy_index/agreement/agreement?id=' + this.list.id + '&title=' + this.list.package_Title
      })
    },

    //是否勾选
    radioGroupChange() {
      this.values = !this.values

    },

    //分享开关
    showIcon() {
      this.popupShow = !this.popupShow
    },
    //分享微信好友
    goPartner(type) {
      let img = this.list.pic_URL
      let title = this.list.package_Title
      let description = this.list.description

      //测试分享链接
      // let shareUrl = 'https://clientv2.yzsheng.com/welfare/welfareH5/details.html?id=' + this.list.id + '&code=' + this.invitationCode+'&t='+new Date().getTime()
      //线上分享链接
      let shareUrl = 'https://ztch5.yzsheng.com/cilentH5/WelfarePurchase/details.html?id=' + this.list.id + '&code=' +
				this.invitationCode + '&t=' + new Date().getTime()
      if (type === 'WXSceneSession' || type === 'WXSenceTimeline') {
        uni.share({
          provider: 'weixin',
          scene: type,
          type: 0,
          summary: description,
          title: title,
          href: shareUrl,
          // href: clientUrl,
          imageUrl: img,
          success: function(res) {

          },
          fail: function(err) {

            uni.showToast({
              title: '分享失败',
              icon: 'none'
            })
          }
        })
      } else if (type === 'qq') {
        uni.share({
          provider: 'qq',
          scene: type,
          type: 0,
          summary: description,
          title: title,
          href: shareUrl,
          // href: clientUrl,
          imageUrl: img,
          success: res => {

          },
          fail: err => {
            console.log(err)
            uni.showToast({
              title: '分享失败',
              icon: 'none'
            })
          }
        })
      } else if (type === 'friends') {
        let url = ''
        // #ifdef H5
        url = shareUrl
        // url = clientUrl
        // #endif
        // #ifndef H5
        url = 'https://ztch5.yzsheng.com/#/pages_buy_index/WelfarePurchase/WelfarePurchase?id=' + this.id + '&t=' + new Date()
          .getTime()
        // url = 'https://clientv2.yzsheng.com/#/pages_buy_index/WelfarePurchase/WelfarePurchase?id=' + this.list.id + '&code=' + this.invitationCode
        // #endif
        uni.navigateTo({
          url: '/pages_buy_index/myfriend/myfriend?img=' + encodeURIComponent(img) + '&title=' + title + '&url=' +
						encodeURIComponent(url)
        })
      }

    },
  }
}
